//
//  EeImagesCellSupport.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/30.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import AsyncDisplayKit
import AVFoundation
import Lantern




open class EeScanSupportKit: RuleCell {
    public var imageView = UIImageView()
    override open func mo_model_reload() {
        let img_url = mo.model["img",""]
        imageView.img_url = img_url
        imageView.backgroundColor = UIColor.table_bg_color
    }
    
}




open class EeImagesScanCell<Kit:EeScanSupportKit>: JoTableCell{
  
    public var imgsView = RuleView<Kit>()
    
    public var browerModels:[EeImageResourceModel] = [EeImageResourceModel]()
    
    open override func addLayoutRules() {
        imgsView.space = (8,8)
        imgsView.limit_width = Swidth/3.0
        imgsView.que_num = 3
        imgsView.padding = [20]
        imgsView.item_scale = 1 //宽高比
    }
    public typealias ItemKit = Kit

    
    open func preview(item: ItemKit) {
        
        let lantern = Lantern()
        let kits = imgsView.cells
        lantern.numberOfItems = {
            return kits.count
        }
        lantern.cellClassAtIndex = { index in
            let kit_ = kits[index]
            let type = kit_.mo.model["type",""]
            switch type {
            case "video":
                return VideoZoomCell.self
            default:
                return LanternImageCell.self
            }
        }
        lantern.reloadCellAtIndex = { context in
            LanternLog.high("reload cell!")
            let kit = kits[context.index]
            let type = kit.mo.model["type",""]

            switch type {
            case "video":
                if let cell = context.cell as? VideoZoomCell{
                    cell.imageView.image = kit.imageView.image
                    if let src = kit.mo.model["fileURL"] as? URL{
                        cell.player.replaceCurrentItem(with: AVPlayerItem(url: src))
                    }
                }
            default:
                if let cell = context.cell as? LanternImageCell{
                    cell.imageView.image = kit.imageView.image

                }
            }
            
        }
        lantern.cellWillAppear = { cell, index in
            let kit = kits[index]
            let type = kit.mo.model["type",""]

            switch type {
            case "video":
                if let cell = cell as? VideoZoomCell{
                    cell.player.play()

                }
            default:
              break
            }

        }
        lantern.cellWillDisappear = { cell, index in
            let kit = kits[index]
            let type = kit.mo.model["type",""]
            switch type {
            case "video":
                if let cell = cell as? VideoZoomCell{
                    cell.player.pause()
                }
            default:
              break
            }
        }
        lantern.transitionAnimator = LanternZoomAnimator(previousView: { index -> UIView? in
            let kit = kits[index]
            return kit.imageView
        })
        lantern.pageIndex = max(item.index, 0)
        lantern.show(method: .present(fromVC: mo.controller, embed: .none))

    }
    

    
    open override func didLayoutRules() {
        imgsView.setContentConstrainToHighLever()
        weak var wself = self
        imgsView.touchAction = { cell,actionID,data in
            let index = data[int:"__index",0]
            if let kit = cell as? Kit{
                wself?.preview(item: kit)
            }
        }
        
    }
    
}



